//
//  PayManager.m
//  MSHD
//
//  Created by HCreater on 2017/12/13.
//  Copyright © 2017年 HCreater. All rights reserved.
//

#import "PayManager.h"

@interface PayManager()

@end

@implementation PayManager

MHSingletonM(DataManager);

- (id)init
{
    self = [super init];
    
    if (self)
    {
        [self initData];
    }
    
    return self;
}

- (void)initData
{
    
}

//模拟支付订单
- (void)easyPay
{
    //    NSDictionary *dict = @{@"appid":@"wx75b78ead0e64a547",@"attach":@"支付测试",@"body":@"APP支付测试",@"mch_id":@"10000100",@"nonce_str":@"1add1a30ac87aa2db72f57a2375d8fec",@"notify_url":@"http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php",@"out_trade_no":@"1415659990",@"spbill_create_ip":@"14.23.150.211",@"total_fee":@"0.1",@"trade_type":@"APP",@"sign":@"0CB01533B8C1EF103065174F50BCA001"};
    //    NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
    
    //调起微信支付
    PayReq* req             = [[PayReq alloc] init];
    req.partnerId           = @"1900000109";
    req.prepayId            = @"WX1217752501201407033233368018";
    req.nonceStr            = @"5K8264ILTKCH16CQ2502SI8ZNMTM67VS";
    req.timeStamp           = 1412000000;
    req.package             = @"Sign=WXPay";
    req.sign                = @"9A0A8659F005D6984697E2CA0A9CF3B7";
    [WXApi sendReq:req];
}

//真实生成订单走这个方法
- (void)bizPay
{
    NSString *res = [self jumpToBizPay];
    if( ![@"" isEqual:res] ){
        UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"支付失败" message:res delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
        
        [alter show];
    }
}

- (NSString *)jumpToBizPay
{
    //============================================================
    // V3&V4支付流程实现
    // 注意:参数配置请查看服务器端Demo
    // 更新时间：2015年11月20日
    //============================================================
    NSString *urlString   = @"http://wxpay.weixin.qq.com/pub_v2/app/app_pay.php?plat=ios";
    //解析服务端返回json数据
    NSError *error;
    //加载一个NSURL对象
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
    //将请求的url数据放到NSData对象中
    NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    if ( response != nil) {
        
        NSMutableDictionary *dict = NULL;
        //IOS5自带解析类NSJSONSerialization从response中解析出数据放到字典中
        dict = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
        
        NSLog(@"url:%@",urlString);
        if(dict != nil){
            NSMutableString *retcode = [dict objectForKey:@"retcode"];
            if (retcode.intValue == 0){
                NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
                
                
                //调起微信支付
                PayReq* req             = [[PayReq alloc] init];
                req.partnerId           = [dict objectForKey:@"partnerid"];
                req.prepayId            = [dict objectForKey:@"prepayid"];
                req.nonceStr            = [dict objectForKey:@"noncestr"];
                req.timeStamp           = stamp.intValue;
                req.package             = [dict objectForKey:@"package"];
                req.sign                = [dict objectForKey:@"sign"];
                [WXApi sendReq:req];
                //日志输出
                NSLog(@"appid=%@\npartid=%@\nprepayid=%@\nnoncestr=%@\ntimestamp=%ld\npackage=%@\nsign=%@",[dict objectForKey:@"appid"],req.partnerId,req.prepayId,req.nonceStr,(long)req.timeStamp,req.package,req.sign );
                return @"";
            }else{
                return [dict objectForKey:@"retmsg"];
            }
        }else{
            return @"服务器返回错误，未获取到json对象";
        }
    }else{
        return @"服务器返回错误";
    }
}

@end
